package com.nbot.server.controller;

import java.util.List;
import java.util.Map;

import com.nbot.server.util.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.nbot.server.domain.AnalogalarmDO;
import com.nbot.server.service.AnalogalarmService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import com.nbot.server.annotation.Log;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-06-24 02:28:18
 */

@Controller
@RequestMapping("/server/analogalarm")
public class AnalogalarmController {
    @Autowired
    private AnalogalarmService analogalarmService;

    @GetMapping()
    @RequiresPermissions("server:analogalarm:analogalarm")
    String Analogalarm() {
        return "server/analogalarm/analogalarm" ;
    }

    @ResponseBody
    @Log("查看")
    @GetMapping("/list")
    @RequiresPermissions("server:analogalarm:analogalarm")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<AnalogalarmDO> analogalarmList = analogalarmService.list(query);
        int total = analogalarmService.count(query);
        PageUtils pageUtils = new PageUtils(analogalarmList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("server:analogalarm:add")
    String add() {
        return "server/analogalarm/add" ;
    }

    @GetMapping("/edit/{autoid}")
    @RequiresPermissions("server:analogalarm:edit")
    String edit(@PathVariable("autoid") Long autoid, Model model) {
            AnalogalarmDO analogalarm = analogalarmService.get(autoid);
        model.addAttribute("analogalarm" , analogalarm);
        return "server/analogalarm/edit" ;
    }

    /**
     * 保存
     */
    @Log("保存")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:analogalarm:add")
    public R save(AnalogalarmDO analogalarm) {
        if (analogalarmService.save(analogalarm) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:analogalarm:edit")
    public R update(AnalogalarmDO analogalarm) {
            analogalarmService.update(analogalarm);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:analogalarm:remove")
    public R remove( Long autoid) {
        if (analogalarmService.remove(autoid) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:analogalarm:batchRemove")
    public R remove(@RequestParam("ids[]") Long[]autoids) {
            analogalarmService.batchRemove(autoids);
        return R.ok();
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public R importExcel(@RequestParam(value = "excelFile", required = false) MultipartFile file, HttpServletRequest request) {
        try {
            MultipartRequest multipartRequest=(MultipartRequest) request;
            MultipartFile excelFile=multipartRequest.getFile("excelFile");
            if(excelFile!=null){
                List<List<String>> datas = ExcelUtil.readXls(excelFile.getInputStream());
                //TODO: 读到的数据都在datas里面，根据实际业务逻辑做相应处理
                System.err.println("hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                if(datas!=null && datas.size()>0){
                    for(int i=0;i<datas.size();i++)
                    {
                        List<String> datai = datas.get(i);
                        int tempInt;
                        AnalogalarmDO anaSave = new AnalogalarmDO();
                        AnalogalarmDO anaUpdate = new AnalogalarmDO();
                        anaSave.setGetdatapath(datai.get(0));//第1列，tag
                        anaSave.setOpcpiontdetail(datai.get(1));//第2列，desc
                        tempInt = (int)Float.parseFloat(datai.get(2));//第3列，报警等级 1-2-3
                        anaSave.setAlarmlevel((long)tempInt);
                        anaSave.setAlarmclass(datai.get(3));//第4列，报警类别 安全类-生产类
                        anaSave.setDirectpoint(datai.get(4));//第5列，站号
                        anaSave.setDirectpointail(datai.get(5));//第6列，位置
                        if(datai.size()>=7)
                            anaSave.setOpcpoint(datai.get(6));//第7列，OPC点名
                        if(datai.size()>=8)
                            anaSave.setRangell(datai.get(7));//第8列，Rangell
                        if(datai.size()>=9) {
                            tempInt = (int) Float.parseFloat(datai.get(8));
                            anaSave.setLlswitch(tempInt);//第9列，Llswitch
                        }
                        if(datai.size()>=10)
                            anaSave.setRangelow(datai.get(9));//第10列，Rangelow
                        if(datai.size()>=11) {
                            tempInt = (int) Float.parseFloat(datai.get(10));
                            anaSave.setLowswitch(tempInt);//第11列，Lowswitch
                        }
                        if(datai.size()>=12)
                            anaSave.setRangehigh(datai.get(11));//第12列，Rangehigh
                        if(datai.size()>=13) {
                            tempInt = (int) Float.parseFloat(datai.get(12));
                            anaSave.setHighswitch(tempInt);//第13列，Highswitch
                        }
                        if(datai.size()>=14)
                            anaSave.setRangehh(datai.get(13));//第14列，Rangehh
                        if(datai.size()>=15) {
                            tempInt = (int) Float.parseFloat(datai.get(14));
                            anaSave.setHhswitch(tempInt);//第15列，Hhswitch
                        }
                        if(datai.size()>=16)
                            anaSave.setUnit(datai.get(15));//第16列，Unit

                        anaSave.setDatatype("analog");

                        anaUpdate = analogalarmService.getByTag(anaSave.getGetdatapath());
                        if (anaUpdate != null) {
                            System.err.println("alarmpiont update!");

                            anaUpdate.setOpcpiontdetail(anaSave.getOpcpiontdetail());
                            anaUpdate.setAlarmlevel(anaSave.getAlarmlevel());
                            anaUpdate.setAlarmclass(anaSave.getAlarmclass());
                            anaUpdate.setDirectpoint(anaSave.getDirectpoint());
                            anaUpdate.setDirectpointail(anaSave.getDirectpointail());
                            analogalarmService.update(anaUpdate);
                        }
                        else {
                            System.err.println("alarmpiont save!");
                            anaSave.setAutoid((long)(analogalarmService.getMaxAutoid()+1));
                            analogalarmService.save(anaSave);
                        }

                    }
                    return R.ok();
                }
                else{
                    return R.error();
                }
            }else{
                return R.error();
            }
        } catch (Exception e) {
            return R.error(-1,e.getMessage());
        }
    }

}
